package com.game.gameHandle
{
	import com.game.gameObject.Player;
	
	import flash.geom.Point;
	
	public class GameMove
	{
		private var tx:Number;			//相对位移x
		private var ty:Number;			//相对位移y
		private var curStep:Number = 0;	//当前步数
		private var steps:Number;		//总的步数
		private var obj:Player;			//移动的对象
		private var startPosition:Point;//初始坐标
		private var direction:Number;	//移动的方向
		
		public function GameMove(obj:Player, endPosition:Point)
		{
			this.obj = obj;
			this.startPosition = new Point(obj.position.x, obj.position.y);
			this.tx = endPosition.x - this.startPosition.x;
			this.ty = endPosition.y - this.startPosition.y;
			this.steps = Math.sqrt(Math.pow(this.tx, 2) + Math.pow(this.ty, 2));
			this.getDir(endPosition);
			
		}
		
		//移动，每帧要循环执行
		public function move():void {
			this.curStep += this.obj.speed/100;
			var x2:Number = this.tx * (this.curStep/this.steps) + this.startPosition.x;
			var y2:Number = this.ty * (this.curStep/this.steps) + this.startPosition.y;
			
			obj.position.x = x2;
			obj.position.y = y2;
			
						
			if (this.curStep >= this.steps) {
				this.stopMove();
			}
		}
		
		//停止移动
		public function stopMove():void {
			this.obj.startMove = false;	
			this.curStep = 0;
		}
		
		//获取方向
		public function getDir(endPosition:Point):void {
			
			var angle:Number = Math.round(this.getAngle(this.startPosition, endPosition));
			
			if(255 < angle && angle < 285){
				this.direction = 0;//6点钟方向
			}
			if(195 < angle && angle < 255){
				this.direction = 1;//7、8点钟方向
			}
			if(165 < angle && angle < 195){
				this.direction = 2;//9点钟方向
			}
			if(105 < angle && angle < 165){
				this.direction = 3;//10、11点钟方向
			}
			if(75 < angle && angle < 105){
				this.direction = 4;//12点钟方向
			}
			if(15 < angle && angle < 75){
				this.direction = 5;//1、2点钟方向
			}
			if(angle < 15 || angle > 345){
				this.direction = 6;//3点钟方向
			}
			if(285 < angle && angle < 345){
				this.direction = 7;//4、5点钟方向
			}
		}

		//求倾斜角
		public function getAngle(startPosition:Point, endPosition:Point):Number {
			var x:Number = endPosition.x - startPosition.x;
			var y:Number = startPosition.y - endPosition.y;
			var angle:Number = Math.atan(y/x)*(180/Math.PI);
			if(x < 0) {
				return angle + 180;
			}else if(y < 0) {
				return angle + 360;
			}
			return angle;
		}
		
		//获取动画贴图Y坐标
		public function getposY():Number {

			var y:Number;
			
			switch(this.direction) {
				case 0: y = 0;break;//6点钟方向
				case 1: y = 0;break;//7、8点钟方向
				case 2: y = this.obj.height*1;break;//9点钟方向
				case 3: y = this.obj.height*3;break;//10、11点钟方向
				case 4: y = this.obj.height*3;break;//12点钟方向
				case 5: y = this.obj.height*3;break;//1、2点钟方向
				case 6: y = this.obj.height*2;break;//3点钟方向
				default: y = 0;break;//4、5点钟方向
			}
			return y;
		}
		
		
	}
}